home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 5.7 KB | 177 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWTMap.tpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWTMap.h"
-
- #ifndef FWERRORS_H
- #include "FWErrors.h"
- #endif
-
- //========================================================================================
- // Class FW_TMap
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_TMap<tKey, tValue>::FW_TMap
- //----------------------------------------------------------------------------------------
-
- template<class tKey, class tValue>
- inline FW_TMap<tKey, tValue>::FW_TMap(FW_SortedArray_CompareFunction compare)
- : fRep(0)
- {
- FW_PlatformError error;
- fRep = FW_PrivSortedArray_New(&error, compare);
- FW_FailOnError(error);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TMap<tKey, tValue>::FW_TMap
- //----------------------------------------------------------------------------------------
-
- template<class tKey, class tValue>
- inline FW_TMap<tKey, tValue>::~FW_TMap()
- {
- for (long i=GetLength()-1; i>=0; --i)
- {
- FW_TPair<tKey, tValue>* item = GetItemAt(i);
- delete item;
- }
- FW_PrivSortedArray_Dispose(fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TMap<tKey, tValue>::GetLength
- //----------------------------------------------------------------------------------------
-
- template<class tKey, class tValue>
- inline long FW_TMap<tKey, tValue>::GetLength() const
- {
- return FW_PrivSortedArray_GetLength(fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TMap<tKey, tValue>::GetItemAt
- //----------------------------------------------------------------------------------------
-
- template<class tKey, class tValue>
- inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::GetItemAt(long index) const
- {
- return (FW_TPair<tKey, tValue>*) FW_PrivSortedArray_GetItemAt(fRep, index);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TMap<tKey, tValue>::Find
- //----------------------------------------------------------------------------------------
-
- template<class tKey, class tValue>
- inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::Find(FW_TPair<tKey, tValue>* item) const
- {
- FW_TPair<tKey, tValue>* result = 0;
- long index;
- FW_Boolean found = FW_PrivSortedArray_Find(fRep, item, &index);
- if (found)
- result = GetItemAt(index);
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TMap<tKey, tValue>::Find
- //----------------------------------------------------------------------------------------
-
- template<class tKey, class tValue>
- inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::Find(const tKey& key) const
- {
- return Find(&FW_TPair<tKey, tValue>(key));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TMap<tKey, tValue>::Add
- //----------------------------------------------------------------------------------------
-
- template<class tKey, class tValue>
- inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::Add(FW_TPair<tKey, tValue>* newItem)
- {
- long index;
- FW_PlatformError error;
- FW_PrivSortedArray_Add(fRep, newItem, &index, &error);
- FW_FailOnError(error);
- return newItem;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TMap<tKey, tValue>::Add
- //----------------------------------------------------------------------------------------
-
- template<class tKey, class tValue>
- inline FW_TPair<tKey, tValue>* FW_TMap<tKey, tValue>::Add(const tKey& key, const tValue& value)
- {
- FW_PlatformError error;
- long index;
- FW_TPair<tKey, tValue>* newItem = new FW_TPair<tKey, tValue>(key, value);
- FW_PrivSortedArray_Add(fRep, newItem, &index, &error);
- FW_FailOnError(error);
- return newItem;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TMap<tKey, tValue>::Remove
- //----------------------------------------------------------------------------------------
-
- template<class tKey, class tValue>
- inline void FW_TMap<tKey, tValue>::Remove(FW_TPair<tKey, tValue>* item)
- {
- FW_PlatformError error;
- long index;
- FW_Boolean found = FW_PrivSortedArray_Find(fRep, item, &index);
- if (found)
- {
- FW_TPair<tKey, tValue>* arrayItem = GetItemAt(index);
- FW_PrivSortedArray_RemoveIndex(fRep, index, &error);
- delete arrayItem;
- }
- else
- error = FW_xBadRemove;
-
- FW_FailOnError(error);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TMap<tKey, tValue>::Remove
- //----------------------------------------------------------------------------------------
-
- template<class tKey, class tValue>
- void FW_TMap<tKey, tValue>::Remove(const tKey& key)
- {
- Remove(&FW_TPair<tKey, tValue>(key));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TMap<tKey, tValue>::operator[]
- //----------------------------------------------------------------------------------------
-
- template<class tKey, class tValue>
- inline tValue& FW_TMap<tKey, tValue>::operator[](const tKey& key)
- {
- FW_PlatformError error;
- FW_TPair<tKey, tValue>* item = 0;
- long index;
- FW_Boolean found = FW_PrivSortedArray_Find(fRep, &FW_TPair<tKey, tValue>(key), &index);
- if (found)
- item = GetItemAt(index);
- else
- {
- item = new FW_TPair<tKey, tValue>(key);
- FW_PrivSortedArray_Add(fRep, item, &index, &error);
- FW_FailOnError(error);
- }
- FW_ASSERT(item != NULL);
- return item->fValue;
- }
-
-